package org.example.comp_algorithm_back.service.impl;

import org.example.comp_algorithm_back.algorithm.CompKey;
import org.example.comp_algorithm_back.algorithm.CompSurvey;
import org.example.comp_algorithm_back.common.CommonResponse;
import org.example.comp_algorithm_back.dto.WordDTO;
import org.example.comp_algorithm_back.service.WordService;
import org.example.comp_algorithm_back.util.DataCleanUtil;
import org.example.comp_algorithm_back.util.PathClass;
import org.example.comp_algorithm_back.vo.WordSearchVo;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;

@Service
public class WordServiceImpl implements WordService {
    @Override
    public CommonResponse<WordSearchVo> survey(WordDTO wordDTO) {
        String word = wordDTO.getWord();
        int choice = wordDTO.getChoice();

        long startTime = System.currentTimeMillis();
        //搜索文件初步清理
        DataCleanUtil.Check();

        WordSearchVo vo = new WordSearchVo();
        vo.setSeedWord(word);
        //竞争度算法运行
        try {
            System.out.println("compkey算法开始，种子关键词为“" + word + "”...");
            CompKey.compkey(word, 20, vo, choice);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }



        //竞争水平评估
        String questionnaire = PathClass.survey + word + PathClass.txt;
        if (new File(questionnaire).exists()) {
            DataCleanUtil.surveyClean(questionnaire, PathClass.wordSurvey, vo.getWordCount());
            CompSurvey.compSurvey(vo, PathClass.wordSurvey, 1, 9);
        }
        long endTime = System.currentTimeMillis();
        long totalTime = endTime - startTime;
        vo.setTime(totalTime);
        return CommonResponse.createForSuccess("查询成功", vo);
    }
}
